package org.gw.building.ctrl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.gw.building.model.*;
import org.gw.building.repositories.*;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.interceptor.RequestPermission;
import org.gw.ylc.base.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by qwx89 on 2017/8/12.
 */
@RestController
@RequestMapping("tqtjgl")
public class TqtjglCtrl {

    @Autowired
    private TqtjkzRepository tqtjkzRepository;

    @Autowired
    private TqtjmxRepository tqtjmxRepository;

    @Autowired
    private TyxxkzRepository tyxxkzRepository;

    @Autowired
    private YxRepository yxRepository;

    @Autowired
    private YxBmRepository yxBmRepository;

    @Autowired
    private TyxxbRepository tyxxbRepository;

    private static final FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd");
    private static final FastDateFormat fdfYear = FastDateFormat.getInstance("yyyy");

    @RequestMapping("zzxx")
    @RequestPermission({RequestPermission.SCHOOL})
    @Transactional
    public List<Tqtjmx> zzxx(Date tjrq) {
        tjrq = tjrq == null ? new Date() : tjrq;
        String day = fdf.format(tjrq);
        Tqtjkz kz = tqtjkzRepository.findByKzrq(day);
        List<Tqtjmx> tjmxs;
        if (kz == null) {
            kz = createTqtjkz(day);
        }
        tjmxs = tqtjmxRepository.findByKzid(kz.getId());
        int tzzs = 0;
        int dytzbs = 0;
        int detzbs = 0;
        int dstzbs = 0;
        int dsitzbs = 0;
        int dwtzbs = 0;
        int sstzbs = 0;
        int bstzbs = 0;
        int zztgs = 0;
        int xstgs = 0;
        int jztgs = 0;
        int bs = 0;
        int ss = 0;
        int bk = 0;
        int eezs = 0;
        int ebzs = 0;
        int swzs = 0;
        int tysbhdy = 0;
        int tysbbhdy = 0;
        int sqrdtys = 0;
        int tys = 0;
        int tyrds = 0;
        int jtyrddtys = 0;
        int fzxtys = 0;
        int ebzsyxqns = 0;
        for (Tqtjmx tjmx : tjmxs) {
            tzzs += tjmx.getTzzs();
            dytzbs += tjmx.getDytzbs();
            detzbs += tjmx.getDetzbs();
            dstzbs += tjmx.getDstzbs();
            dsitzbs += tjmx.getDsitzbs();
            dwtzbs += tjmx.getDwtzbs();
            sstzbs += tjmx.getSstzbs();
            bstzbs += tjmx.getBstzbs();
            zztgs += tjmx.getZztgs();
            xstgs += tjmx.getXstgs();
            jztgs += tjmx.getJztgs();
            bs += tjmx.getBs();
            ss += tjmx.getSs();
            bk += tjmx.getBk();
            eezs += tjmx.getEezs();
            ebzs += tjmx.getEbzs();
            swzs += tjmx.getSwzs();
            tysbhdy += tjmx.getTysbhdy();
            tysbbhdy += tjmx.getTysbbhdy();
            sqrdtys += tjmx.getSqrdtys();
            tys += tjmx.getTys();
            tyrds += tjmx.getTyrds();
            jtyrddtys += tjmx.getJtyrddtys();
            fzxtys += tjmx.getFzxtys();
            ebzsyxqns += tjmx.getEbzsyxqns();
        }
        Yx yx = new Yx();
        yx.setMc("总计");
        Tqtjmx tqtjmx = Tqtjmx.builder().kzid(kz.getId()).yx(yx).tzzs(tzzs).
                dytzbs(dytzbs).detzbs(detzbs).dstzbs(dstzbs).dsitzbs(dsitzbs).dwtzbs(dwtzbs).
                sstzbs(sstzbs).bstzbs(bstzbs).zztgs(zztgs).xstgs(xstgs).jztgs(jztgs).bs(bs).
                ss(ss).bk(bk).eezs(eezs).ebzs(ebzs).swzs(swzs).tysbhdy(tysbhdy).tysbbhdy(tysbbhdy).
                sqrdtys(sqrdtys).tys(tys).tyrds(tyrds).jtyrddtys(jtyrddtys).fzxtys(fzxtys).ebzsyxqns(ebzsyxqns).
                build();
        tjmxs.add(tqtjmx);
        return tjmxs;
    }

    private Tqtjkz createTqtjkz(String day) {
        UserInfo userInfo = RequestContextHolder.get().getUser();
        Tqtjkz tqtjkz = Tqtjkz.builder().kzmc(day).kzrq(day).kzr(userInfo.getId()).build();
        tqtjkz = tqtjkzRepository.save(tqtjkz);
        List<Yx> yxs = yxRepository.findBySfyx(true);
        if (CollectionUtils.isEmpty(yxs)) {
            return tqtjkz;
        }
        List<Tqtjmx> tjmxs = new ArrayList<>();
        for (Yx yx : yxs) {
            List<YxBm> bms = yxBmRepository.findByYx(yx);
            int tzzs = 0;
            int dytzbs = 0;
            int detzbs = 0;
            int dstzbs = 0;
            int dsitzbs = 0;
            int dwtzbs = 0;
            int sstzbs = 0;
            int bstzbs = 0;
            int zztgs = 0;
            int xstgs = 0;
            int jztgs = 0;
            int bs = 0;
            int ss = 0;
            int bk = 0;
            int eezs = 0;
            int ebzs = 0;
            int swzs = 0;
            int tysbhdy = 0;
            int tysbbhdy = 0;
            int sqrdtys = 0;
            int tys = 0;
            int tyrds = 0;
            int jtyrddtys = 0;
            int fzxtys = 0;
            int ebzsyxqns = 0;
            String[] rqArr = StringUtils.split(day, "-");
            int queryYear = Integer.valueOf(rqArr[0]);
            Integer nowYear = Integer.valueOf(fdfYear.format(new Date()));
            for (YxBm yxbm : bms) {
                Bm bm = yxbm.getBm();
                if (StringUtils.equals("3", bm.getCc().getDm())) {
                    tzzs++;
                } else if (StringUtils.equals("4", bm.getCc().getDm())) {
                    String lbdm = bm.getLb().getDm();
                    if (StringUtils.equals("1", lbdm)) {//本科生团支部
                        int jlny = bm.getJlny();
                        //判断大几 0-4 ： 大一到大五
                        int a = Integer.valueOf(rqArr[0]) - jlny + (Integer.valueOf(rqArr[1]) > 7 ? 0 : -1);
                        switch (a) {
                            case 0:
                                dytzbs++;
                                break;
                            case 1:
                                detzbs++;
                                break;
                            case 2:
                                dstzbs++;
                                break;
                            case 3:
                                dsitzbs++;
                                break;
                            case 4:
                                dwtzbs++;
                                break;
                            default:
                                break;
                        }
                    } else if (StringUtils.equals("2", lbdm)) {//硕士生团支部
                        sstzbs++;
                    } else if (StringUtils.equals("3", lbdm)) {//博士生团支部
                        bstzbs++;
                    }
                }
                List<Tyxxb> tyxxbs = tyxxbRepository.findByZbmcAndScbz(bm, false);
                for (Tyxxb ty : tyxxbs) {
                    String rylx = ty.getLx().getDm();//用户类型 1 学生 2 教师
                    boolean tgb = ty.isTgb();
                    if (StringUtils.equals(rylx, "1")) {//学生
                        if (tgb) {
                            xstgs++;
                        }
                        String zzmm = ty.getZzmm().getDm();//01 中共党员 02中共预备党员 03 共青团员 13 群众
                        if (StringUtils.equals(zzmm, "01")) {
                            tysbhdy++;
                        } else if (StringUtils.equals(zzmm, "02")) {
                            tysbhdy++;
                        } else if (StringUtils.equals(zzmm, "03")) {
                            tysbhdy++;
                            tysbbhdy++;
                        }
                        Date sqrdrq = ty.getSqrdrq();
                        if (sqrdrq != null && queryYear - Integer.valueOf(fdfYear.format(sqrdrq)) == 0) {
                            sqrdtys++;
                        }
                        Date tyrq = ty.getTntyrq();
                        if (tyrq != null && queryYear - Integer.valueOf(fdfYear.format(tyrq)) == 0) {
                            tys++;
                        }
                        Date fzwybdyrq = ty.getFzwybdyrq();
                        if (fzwybdyrq != null && queryYear - Integer.valueOf(fdfYear.format(fzwybdyrq)) == 0) {
                            tyrds++;
                            if (tyrq != null) {
                                jtyrddtys++;
                            }
                        }
                        Date rtrq = ty.getRtrq();
                        if (rtrq != null && queryYear - Integer.valueOf(fdfYear.format(rtrq)) == 0) {
                            fzxtys++;
                        }
                    } else if (StringUtils.equals(rylx, "2")) {//教师
                        int a = tgb ? (zztgs++) : (jztgs++);
                    }
                    String whcd = ty.getWhcd().getDm();//11 博士 14 硕士 21 本科
                    if (StringUtils.equals(whcd, "11")) {
                        bs++;
                    } else if (StringUtils.equals(whcd, "14")) {
                        ss++;
                    } else if (StringUtils.equals(whcd, "21")) {
                        bk++;
                    }
                    Date csrq = ty.getCsrq();
                    if (csrq != null) {
                        int year = nowYear - Integer.valueOf(fdfYear.format(csrq));
                        if (year > 35) {
                            swzs++;
                        } else if (year > 28) {
                            ebzs++;
                        } else if (year >= 22) {
                            if (StringUtils.equals(rylx, "1")) {
                                ebzsyxqns++;
                            }
                            eezs++;
                        }
                    }
                }
            }
            Tqtjmx tqtjmx = Tqtjmx.builder().kzid(tqtjkz.getId()).yx(yx).tzzs(tzzs).
                    dytzbs(dytzbs).detzbs(detzbs).dstzbs(dstzbs).dsitzbs(dsitzbs).dwtzbs(dwtzbs).
                    sstzbs(sstzbs).bstzbs(bstzbs).zztgs(zztgs).xstgs(xstgs).jztgs(jztgs).bs(bs).
                    ss(ss).bk(bk).eezs(eezs).ebzs(ebzs).swzs(swzs).tysbhdy(tysbhdy).tysbbhdy(tysbbhdy).
                    sqrdtys(sqrdtys).tys(tys).tyrds(tyrds).jtyrddtys(jtyrddtys).fzxtys(fzxtys).ebzsyxqns(ebzsyxqns).
                    build();
            tjmxs.add(tqtjmx);
        }
        tqtjmxRepository.save(tjmxs);
        return tqtjkz;
    }
}
